package com.west.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.west.common.common.R;

/**
 * @author hanyulang
 * @date 2018/11/19
 */
public class ThreeDotView extends View {

    private Paint mPaint;
    private int mDistance = 6;
    private int mRadius = 6;
    private float mCenterY;
    private int mCount = 3;
    private int mColor = Color.parseColor("#E1E1E1");

    public ThreeDotView(Context context) {
        super(context);
        init(null);
    }

    public ThreeDotView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public ThreeDotView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs){

        if(attrs != null){
            TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.DotsView);
            mColor = array.getColor(R.styleable.DotsView_dot_fill_color, mColor);
            mCount = array.getInt(R.styleable.DotsView_dot_count, mCount);
            array.recycle();
        }

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(mColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = mRadius * 2 * mCount + (mCount - 1) * mDistance;
        setMeasuredDimension(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(mRadius * 2, MeasureSpec.EXACTLY));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mCenterY = getHeight() * 0.5f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for(int i = 0; i < mCount; i++){
            canvas.drawCircle(mRadius * (2 * i + 1) + mDistance * i, mCenterY, mRadius, mPaint);
        }
    }

}
